package com.cms.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.text.AbstractDocument.Content;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cms.entity.ArcType;
import com.cms.entity.Article;
import com.cms.entity.Comment;
import com.cms.service.ArticleService;
import com.cms.service.CommentService;
import com.cms.util.NavUtil;
import com.cms.util.StringUtil;

/**
 * 文章Controller层
 * @author user
 *
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

	

	@Resource
	private ArticleService articleService;

	@Resource
	private CommentService commentService;

	
	
	@RequestMapping("/{id}")
	public String details(@PathVariable("id") Integer id,Model model,HttpServletRequest request)throws Exception{		
		Article article=articleService.findById(id);
		List<Comment> comments = commentService.getComments(article);
		System.out.println("=======comments");
		for(Comment comment: comments) {
			System.out.println(comment);
		}
		
		model.addAttribute("article", article);	
		model.addAttribute("comments", comments);
		
		ArcType arcType=article.getArcType();//文章对象中包含类别对象
		
		model.addAttribute("navCode",NavUtil.genArticleNavigation(arcType.getTypeName(), arcType.getId(), article.getTitle()));
		
		String keyWords=article.getKeyWords();
		if(StringUtil.isNotEmpty(keyWords)){
			String arr[]=keyWords.split(" ");
			model.addAttribute("keyWords", StringUtil.filterWhite(Arrays.asList(arr)));
		}else{
			model.addAttribute("keyWords", null);
		}
		
		article.setClick(article.getClick()+1);
		articleService.update(article);
		model.addAttribute("pageCode", this.genUpAndDownPageCode(articleService.getLastArticle(id,arcType.getId()), articleService.getNextArticle(id,arcType.getId()), request.getServletContext().getContextPath()));

		return "article";
	}
	
	
	
	/**
	 * 获取下一篇文章和上一篇文章代码
	 * @param lastArticle
	 * @param nextArticle
	 * @param projectContext
	 * @return
	 */
	public String genUpAndDownPageCode(Article lastArticle,Article nextArticle,String projectContext){
		StringBuffer pageCode=new StringBuffer();
		if(lastArticle==null || lastArticle.getId()==null){
			pageCode.append("<p>上一篇：没有了</p>");
		}else{
			pageCode.append("<p>上一篇：<a href='"+projectContext+"/article/"+lastArticle.getId()+".html'>"+lastArticle.getTitle()+"</a></p>");
		}
		
		if(nextArticle==null || nextArticle.getId()==null){
			pageCode.append("<p>下一篇：没有了</p>");
		}else{
			pageCode.append("<p>下一篇：<a href='"+projectContext+"/article/"+nextArticle.getId()+".html'>"+nextArticle.getTitle()+"</a></p>");
		}
		return pageCode.toString();
	}

	
	
	
}

